import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ApiProvider } from "../../providers/api/api";
import { UnitCourseInfoPage } from "../unit-course-info/unit-course-info";
import { UnitSourceListPage } from "../unit-source-list/unit-source-list";
import { LocalStorProvider } from "../../providers/local-stor/local-stor";
import { TeachingPage } from "../teaching/teaching";

/**
 * Generated class for the UnitListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-unit-list',
  templateUrl: 'unit-list.html',
})
export class UnitListPage {
  classCourseId;
  teacherId;
  courseName;

  unitList;
  courseInfo;
  unitCount;

  studentId;

  operateType;

  courseId;

  classcourse_num;
  courseclassid;//课程id
  unitId;//单元id
  sta;//判断授课还是继续授课
  is_s;

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: ApiProvider, public storage: LocalStorProvider) {
  }

  ionViewDidLoad() {
    /*    this.storage.getKey("user_id").then(val=>{
          alert(val)
          this.teacherId=val;
        });

        this.classCourseId=this.navParams.get("classCourseId");
        this.courseName=this.navParams.get("courseName");
        this.operateType=this.navParams.get("operateType");
        this.courseId=this.navParams.get("courseId");

        this.getUnitList();*/
  }

  ionViewDidEnter() {
    this.storage.getKey("user_id").then(val => {
      this.teacherId = val;

      this.classCourseId = this.navParams.get("classCourseId");
      this.courseName = this.navParams.get("courseName");
      this.operateType = this.navParams.get("operateType");
      this.courseId = this.navParams.get("courseId");
      this.classcourse_num = this.navParams.get("classcourse_num");
      this.courseclassid = this.navParams.get("courseclassid");
      this.unitId = this.navParams.get("unitId");

      this.sta = this.navParams.get("sta");
      this.is_s = this.navParams.get("is_s");




      this.getUnitList();
    });
  }

  /**
   * 获取单元列表
   */
  getUnitList() {
    this.http.post("/unitTeacherController/unitListByCourseIdofTeacher.do", { Classcourseid: +this.classCourseId, userId: +this.teacherId, courseId: this.courseId }).subscribe(item => {
      this.unitList = item["obj"];
      console.log(123)
      console.log(this.unitList)
      //this.courseInfo=item["courseInfo"];
      this.unitCount = item["obj"].length;
    });
  }

  /**
   * 课程详情页面
   */
  getcourseInfo() {
    this.navCtrl.push(UnitCourseInfoPage, {
      courseName: this.courseName, unitCount: this.unitCount, className: this.courseInfo["className"],
      classend: this.courseInfo["classend"], examplan: this.courseInfo["examplan"]
    });
  }

  /**
   * 跳转至单元下的资源习题列表
   * @param unitId
   */
  showSource(unitId) {
    localStorage.setItem("class_type", "60001");

    this.navCtrl.push(UnitSourceListPage, {
      classCourseId: this.classCourseId, teacherId: this.teacherId, unitId: unitId, studentId: this.studentId,
      classType: "60001", sourceType: 'xt'
    });
  }

  /**
   *
   * @param unitId
   */
  operateUnit(unitId) {
    //operateType为1时，进入授课操作页面
    localStorage.setItem("unitId",unitId);

      this.sta = this.navParams.get("sta");
    if (this.operateType == 1) {
      this.navCtrl.push(TeachingPage, { className: this.courseName, classcourseId: this.classCourseId,
        classcourse_num: this.classcourse_num, teacherId: this.teacherId,unitId:unitId,sta:this.sta,is_s:this.is_s});
    }
  }
}
